<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/5/24
 * Time: 17:14
 */

namespace App\Base;


use App\Base\Form\Article;
use App\Base\Form\BaseType;
use App\Base\Form\Checkbox;
use App\Base\Form\Date;
use App\Base\Form\DateTime;
use App\Base\Form\Entity;
use App\Base\Form\File;
use App\Base\Form\Files;
use App\Base\Form\Hidden;
use App\Base\Form\Lists;
use App\Base\Form\Pic;
use App\Base\Form\Pics;
use App\Base\Form\Radio;
use App\Base\Form\Select;
use App\Base\Form\SelectInput;
use App\Base\Form\Swicth;
use App\Base\Form\Text;
use App\Base\Form\Textarea;
use App\Base\Form\Time;

class BaseForm
{
    public $data = [];//总数据
    public $entity;//数组形式的实体信息
    public $defaultRowWidth = 12;//col-md-12
    public $labelPosition = "top";//表单标签的位置
    public $formConfig = [];//表单配置数据
    public $form = [];//表单数据
    public $class = null;

    public function __construct($class = null)
    {
        $this->class = $class;
    }

    public function setLabelPosition($position)
    {
        $this->labelPosition = $position;
        return $this;
    }

    public function setRowWidth($width)
    {
        $this->defaultRowWidth = $width;
        return $this;
    }

    public function setEntity(array $entity)
    {
        $this->entity = $entity;
        return $this;
    }


    public function createForm()
    {
        $this->data["entity"] = $this->entity;
        $this->data["defaultRowWidth"] = $this->defaultRowWidth;
        $this->data["labelPosition"] = $this->labelPosition;
        //根据配置信息返回表单数据
        foreach ($this->formConfig as $name=>$conf) {
            $this->form[$name] = $conf->getData();
        }
        $this->data["form"] = $this->form;
        return $this->data;
    }

    /**
     * @param $name
     * @param null $choice
     * @return mixed|Article|Text|Select
     */
    public function addItem($name,$choice = null)
    {
        $data = getAnnotationData($this->class,$name);
        $label = $data->cnName;
        $type = ucfirst($data->editType);
        $func = "add{$type}";
        return $this->$func($name,$label,$choice);
    }

    /**
     * @param $name
     * @param $label
     * @return Article
     */
    public function addArt($name,$label="")
    {
       return $this->formConfig[$name] = new Article($name,$label);
    }
    /**
     * @param $name
     * @param $label
     * @return Article
     */
    public function addArticle($name,$label)
    {
       return $this->formConfig[$name] = new Article($name,$label);
    }

    /**
     * @param $name
     * @param $label
     * @param $choice
     * @return Checkbox
     */
    public function addCheckbox($name,$label,$choice)
    {
        return $this->formConfig[$name] = new Checkbox($name,$label,$choice);
    }

    /**
     * @param $name
     * @param $label
     * @param $choice
     * @return Swicth
     */
    public function addSwitch($name,$label,$choice=[])
    {
        return $this->formConfig[$name] = new Swicth($name,$label,$choice);
    }

    /**
     * @param $name
     * @param $label
     * @param $choice
     * @return Radio
     */
    public function addRadio($name,$label,$choice)
    {
        return $this->formConfig[$name] = new Radio($name,$label,$choice);
    }

    /**
     * @param $name
     * @param $label
     * @param $choice
     * @return Select
     */
    public function addSelect($name,$label,$choice)
    {
        return $this->formConfig[$name] = new Select($name,$label,$choice);
    }

    /**
     * @param $name
     * @param $label
     * @param $choice
     * @return SelectInput
     */
    public function addSelectinput($name,$label,$choice)
    {
        return $this->formConfig[$name] = new SelectInput($name,$label,$choice);
    }

    /**
     * @param $name
     * @param $label
     * @return Date
     */
    public function addDate($name,$label)
    {
        return $this->formConfig[$name] = new Date($name,$label);
    }

    /**
     * @param $name
     * @param $label
     * @return DateTime
     */
    public function addDatetime($name,$label)
    {
        return $this->formConfig[$name] = new DateTime($name,$label);
    }

    /**
     * @param $name
     * @param $label
     * @return Time
     */
    public function addTime($name,$label)
    {
        return $this->formConfig[$name] = new Time($name,$label);
    }

    /**
     * @param $name
     * @param $label
     * @return File
     */
    public function addFile($name,$label)
    {
        return $this->formConfig[$name] = new File($name,$label);
    }

    /**
     * @param $name
     * @param $label
     * @return Files
     */
    public function addFiles($name,$label)
    {
        return $this->formConfig[$name] = new Files($name,$label);
    }

    /**
     * @param $name
     * @param $label
     * @return Pic
     */
    public function addPic($name,$label)
    {
        return $this->formConfig[$name] = new Pic($name,$label);
    }

    /**
     * @param $name
     * @param $label
     * @return Pics
     */
    public function addPics($name,$label)
    {
        return $this->formConfig[$name] = new Pics($name,$label);
    }

    /**
     * @param $name
     * @param $label
     * @return Text
     */
    public function addText($name,$label)
    {
        return $this->formConfig[$name] = new Text($name,$label);
    }

    /**
     * @param $name
     * @param $label
     * @return Hidden
     */
    public function addHidden($name,$label)
    {
        return $this->formConfig[$name] = new Hidden($name,$label);
    }

    /**
     * @param $name
     * @param $label
     * @return Textarea
     */
    public function addTextarea($name,$label)
    {
        return $this->formConfig[$name] = new Textarea($name,$label);
    }

    /**
     * @param $name
     * @param $label
     * @param $choice
     * @return Entity
     */
    public function addEntity($name,$label,$choice = [])
    {
        return $this->formConfig[$name] = new Entity($name,$label,$choice);
    }

    /**
     * @param $name
     * @param $label
     * @param $choice
     * @return Lists
     */
    public function addList($name,$label,$choice = [])
    {
        return $this->formConfig[$name] = new Lists($name,$label,$choice);
    }
}
